#coding=utf-8
#Boa:Dialog:DlgCustRegionEditor

import wx

from product import CDataCustomerRegion
import kit
from ctrlAider import *
from product import *

def create(parent):
    return DlgCustRegionEditor(parent)

[wxID_DLGCUSTREGIONEDITOR, wxID_DLGCUSTREGIONEDITORBTNCANCEL, 
 wxID_DLGCUSTREGIONEDITORBTNCONFIRM, wxID_DLGCUSTREGIONEDITORCBSALESMAN, 
 wxID_DLGCUSTREGIONEDITORSTATICTEXT1, wxID_DLGCUSTREGIONEDITORSTATICTEXT10, 
 wxID_DLGCUSTREGIONEDITORSTATICTEXT12, wxID_DLGCUSTREGIONEDITORSTATICTEXT2, 
 wxID_DLGCUSTREGIONEDITORSTATICTEXT3, wxID_DLGCUSTREGIONEDITORSTATICTEXT4, 
 wxID_DLGCUSTREGIONEDITORSTATICTEXT5, wxID_DLGCUSTREGIONEDITORSTATICTEXT6, 
 wxID_DLGCUSTREGIONEDITORSTATICTEXT7, wxID_DLGCUSTREGIONEDITORSTATICTEXT8, 
 wxID_DLGCUSTREGIONEDITORSTATICTEXT9, wxID_DLGCUSTREGIONEDITORTCCODE, 
 wxID_DLGCUSTREGIONEDITORTCDISTANCE, wxID_DLGCUSTREGIONEDITORTCDRIVETIME, 
 wxID_DLGCUSTREGIONEDITORTCIMPORTANCE, wxID_DLGCUSTREGIONEDITORTCLEVEL, 
 wxID_DLGCUSTREGIONEDITORTCMARKETSHARE, wxID_DLGCUSTREGIONEDITORTCMEMO, 
 wxID_DLGCUSTREGIONEDITORTCNAME, wxID_DLGCUSTREGIONEDITORTCODE, 
 wxID_DLGCUSTREGIONEDITORTCROUTE, wxID_DLGCUSTREGIONEDITORTCSPAN, 
 wxID_DLGCUSTREGIONEDITORTNAME, 
] = [wx.NewId() for _init_ctrls in range(27)]

class DlgCustRegionEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGCUSTREGIONEDITOR,
              name=u'DlgCustRegionEditor', parent=prnt, pos=wx.Point(949, 342),
              size=wx.Size(543, 520), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u7f16\u8f91\u5730\u533a')
        self.SetClientSize(wx.Size(527, 482))
        self.Center(wx.BOTH)
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))

        self.tName = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORTNAME,
              label=u'\u540d\u79f0\uff1a', name=u'tName', parent=self,
              pos=wx.Point(48, 40), size=wx.Size(51, 19), style=0)

        self.tcName = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCNAME,
              name=u'tcName', parent=self, pos=wx.Point(104, 32),
              size=wx.Size(184, 28), style=0, value=u'')
        self.tcName.Bind(wx.EVT_TEXT, self.OnTcNameText,
              id=wxID_DLGCUSTREGIONEDITORTCNAME)

        self.staticText1 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT1,
              label=u'\u8bf4\u660e\uff1a', name='staticText1', parent=self,
              pos=wx.Point(48, 96), size=wx.Size(51, 19), style=0)

        self.tcMemo = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCMEMO,
              name=u'tcMemo', parent=self, pos=wx.Point(104, 88),
              size=wx.Size(384, 28), style=0, value=u'')

        self.btnConfirm = wx.Button(id=wxID_DLGCUSTREGIONEDITORBTNCONFIRM,
              label=u'\u786e\u5b9a', name=u'btnConfirm', parent=self,
              pos=wx.Point(120, 424), size=wx.Size(86, 29), style=0)
        self.btnConfirm.Bind(wx.EVT_BUTTON, self.OnBtnConfirmButton,
              id=wxID_DLGCUSTREGIONEDITORBTNCONFIRM)

        self.btnCancel = wx.Button(id=wxID_DLGCUSTREGIONEDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(304, 424), size=wx.Size(86, 29), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGCUSTREGIONEDITORBTNCANCEL)

        self.tCode = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORTCODE,
              label=u'\u7b80\u7801\uff1a', name=u'tCode', parent=self,
              pos=wx.Point(328, 40), size=wx.Size(51, 19), style=0)

        self.tcCode = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCCODE,
              name=u'tcCode', parent=self, pos=wx.Point(384, 32),
              size=wx.Size(100, 28), style=0, value=u'')

        self.staticText2 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT2,
              label=u'\u9500\u91cf\u7b49\u7ea7\uff1a', name='staticText2',
              parent=self, pos=wx.Point(16, 152), size=wx.Size(85, 19),
              style=0)

        self.tcLevel = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCLEVEL,
              name=u'tcLevel', parent=self, pos=wx.Point(104, 144),
              size=wx.Size(100, 28), style=0, value=u'')

        self.staticText3 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT3,
              label=u'\u9500\u91cf\u8bc4\u4f30 \u9ad8-A \u4e2d-B \u4f4e-C',
              name='staticText3', parent=self, pos=wx.Point(216, 152),
              size=wx.Size(180, 19), style=0)

        self.staticText4 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT4,
              label=u'\u9001\u8d27\u95f4\u9694\uff1a', name='staticText4',
              parent=self, pos=wx.Point(16, 296), size=wx.Size(85, 19),
              style=0)

        self.tcSpan = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCSPAN,
              name=u'tcSpan', parent=self, pos=wx.Point(104, 288),
              size=wx.Size(64, 28), style=0, value=u'0')

        self.staticText5 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT5,
              label=u'\u91cd\u8981\u7a0b\u5ea6\uff1a', name='staticText5',
              parent=self, pos=wx.Point(16, 200), size=wx.Size(85, 19),
              style=0)

        self.tcImportance = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCIMPORTANCE,
              name=u'tcImportance', parent=self, pos=wx.Point(104, 192),
              size=wx.Size(100, 28), style=0, value=u'')

        self.staticText6 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT6,
              label=u'\u9001\u8d27\u7ebf\u8def\u4e0a\u7684\uff0c\u5747\u4e3a\u6700\u9ad8\u7ea7\uff0c\u5168\u529b\u4fdd\u969c',
              name='staticText6', parent=self, pos=wx.Point(216, 200),
              size=wx.Size(289, 19), style=0)

        self.staticText7 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT7,
              label=u'\u5bfc\u822a\u8ddd\u79bb\uff1a', name='staticText7',
              parent=self, pos=wx.Point(16, 248), size=wx.Size(85, 19),
              style=0)

        self.tcDistance = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCDISTANCE,
              name=u'tcDistance', parent=self, pos=wx.Point(104, 240),
              size=wx.Size(100, 28), style=0, value=u'')

        self.staticText8 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT8,
              label=u'\u5bfc\u822a\u7528\u65f6\uff1a', name='staticText8',
              parent=self, pos=wx.Point(224, 248), size=wx.Size(85, 19),
              style=0)

        self.tcDriveTime = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCDRIVETIME,
              name=u'tcDriveTime', parent=self, pos=wx.Point(312, 240),
              size=wx.Size(100, 28), style=0, value=u'')

        self.staticText9 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT9,
              label=u'\u6211\u65b9\u4efd\u989d\uff1a', name='staticText9',
              parent=self, pos=wx.Point(16, 344), size=wx.Size(85, 19),
              style=0)

        self.staticText10 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT10,
              label=u'\u9002\u5b9c\u8def\u7ebf\uff1a', name='staticText10',
              parent=self, pos=wx.Point(224, 296), size=wx.Size(85, 19),
              style=0)

        self.tcRoute = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCROUTE,
              name=u'tcRoute', parent=self, pos=wx.Point(312, 288),
              size=wx.Size(100, 28), style=0, value=u'')

        self.tcMarketShare = wx.TextCtrl(id=wxID_DLGCUSTREGIONEDITORTCMARKETSHARE,
              name=u'tcMarketShare', parent=self, pos=wx.Point(104, 336),
              size=wx.Size(64, 28), style=0, value=u'')

        self.staticText12 = wx.StaticText(id=wxID_DLGCUSTREGIONEDITORSTATICTEXT12,
              label=u'\u5f00\u53d1\u4e13\u5458\uff1a', name='staticText12',
              parent=self, pos=wx.Point(224, 344), size=wx.Size(85, 19),
              style=0)

        self.cbSalesman = wx.ComboBox(choices=[],
              id=wxID_DLGCUSTREGIONEDITORCBSALESMAN, name=u'cbSalesman',
              parent=self, pos=wx.Point(312, 344), size=wx.Size(145, 27),
              style=0, value=u'')
        self.cbSalesman.SetLabel(u'')

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.m_region = None

        self.ctrlSalesman = MyComboBoxDecorator(self.cbSalesman, CDataStaff, CDataStaff.COL_NAME, only_enable_staff)
        self.ctrlSalesman.refresh()

    def setEditData(self, regionObj):
        self.m_region = regionObj
        self.tcName.SetValue(regionObj.getValue(regionObj.fi_name))
        self.tcCode.SetValue(regionObj.getValue(regionObj.fi_code))
        self.tcMemo.SetValue(regionObj.getValue(regionObj.fi_memo))
        self.tcLevel.SetValue(str(regionObj.getValue(regionObj.fi_level)))
        self.tcSpan.SetValue(str(regionObj.getValue(regionObj.fi_delivery_interval)))
        self.tcDistance.SetValue(str(regionObj.getValue(regionObj.fi_distance)))
        self.tcDriveTime.SetValue(str(regionObj.getValue(regionObj.fi_drive_time)))
        self.tcMarketShare.SetValue(str(regionObj.getValue(regionObj.fi_market_share)))
        #self.tcS.SetValue(str(regionObj.getValue(regionObj.fi_salesman)))
        self.ctrlSalesman.SetValue(CDataStaff.getName(regionObj.getFieldVal(index=regionObj.fi_salesman)))
        self.tcImportance.SetValue(str(regionObj.getValue(regionObj.fi_importance)))
        self.tcRoute.SetValue(str(regionObj.getValue(regionObj.fi_route)))
        
    def OnBtnConfirmButton(self, event):
        name = self.tcName.GetValue()
        if (name == ''):
            dlg = wx.MessageDialog(self, u'必须设置区域名称', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        saleObj = self.ctrlSalesman.getSelectObj()
        saleId = None if saleObj is None else saleObj.getValue(CDataStaff.COL_ID)
        
        data = {
            CDataCustomerRegion.fi_name: self.tcName.GetValue(),
            CDataCustomerRegion.fi_code: self.tcCode.GetValue(),
            CDataCustomerRegion.fi_memo: self.tcMemo.GetValue(),
            CDataCustomerRegion.fi_delivery_interval: self.tcSpan.GetValue(),
            CDataCustomerRegion.fi_level: self.tcLevel.GetValue(),
            CDataCustomerRegion.fi_distance: self.tcDistance.GetValue(),
            CDataCustomerRegion.fi_drive_time: self.tcDriveTime.GetValue(),
            CDataCustomerRegion.fi_market_share: self.tcMarketShare.GetValue(),
            CDataCustomerRegion.fi_importance: self.tcImportance.GetValue(),
            CDataCustomerRegion.fi_route: self.tcRoute.GetValue(),
            CDataCustomerRegion.fi_salesman:saleId
        }


        if self.m_region:
            self.m_region.setMulFieldVal(data)
            self.m_region.update()
        else:
            #CDataCustomerRegion.add(name, self.tcMemo.GetValue(), self.tcCode.GetValue()
            #    , self.tcSpan.GetValue(), self.tcLevel.GetValue())
            obj = CDataCustomerRegion.factory()
            obj.setMulFieldVal(data)
            obj.insert()
            self.curObj = obj
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)

    def OnTcNameText(self, event):
        import config
        name = self.tcName.GetValue()
        code = config.CBriefCode.getBriefCode(name)
        self.tcCode.SetValue(code)
